<?php

class Home extends BZ_Controller
{
    public function _init()
    {
        $this->load->helper(array('city', 'common'));
        $this->load->model('advert_model', 'advert');
        $this->load->model('scenery_base_model', 'scenery_base');
        $this->load->model('region_model', 'region');
        $this->load->model('tourism_goods_base_model', 'tourism_goods_base');
        $this->load->model('scenic_category_model', 'scenic_category');
        $this->load->model('tourism_key_word_model', 'tourism_key_word');
        $this->load->model('news_content_model', 'news_content');
        $this->load->model('news_class_model', 'news_class');
        $this->load->model('activity_lottery_model', 'activity_lottery');
        $this->load->model('promotion_project_model', 'promotion_project');
        $this->load->model('line_base_model', 'line_base');
    }

    /**
     * @descripte   大首页
     * @Author xiumao
     * @date 2016/7/15 下午 4:25
     */
    public function index()
    {
        if (!$this->cache->memcached->get('wapindexHomePageCache')) {
            $data = array(
                'advert'       => $this->advert->findByParams(['status'=> 1, 'opentype' => 'link', 'position' => 8], '*', null, 'sort ASC')->result(), //头部轮播
                'news_content' => $this->news_content->page_list($page_num = 10, $num = 0, $param = array())->result(),
            );
            $this->cache->memcached->save('wapindexHomePageCache', $data, 3 * 24 * 3600);
        } else {
            $data = $this->cache->memcached->get('wapindexHomePageCache');
        }
        $params['region_name'] = get_cookie('city') ? get_cookie('city') : '杭州';
        $params['region_type'] = 2;
        $result = $this->region->findParams($params);
        if ($result->num_rows() > 0) {
            $data['province_id'] = $result->row()->parent_id;
            $data['region_id'] = $result->row()->region_id;
        } else {
            $data['province_id'] = "31";
            $data['region_id'] = "383";
            $params['region_name'] = '杭州';
        }
        $data['region_name'] = $params['region_name'];
        $this->load->view('home/home/grid', $data);
    }

    /**
     * third edition index
     */
    public function grid()
    {

        if (!$this->cache->memcached->get('wapindexHomePageCache')) {
            $data = array(
                'advert'        => $this->advert->findByParams(['status'=> 1, 'opentype' => 'link', 'position' => 8], '*', null, 'sort ASC')->result(), //头部轮播
                'valuePurchase' => $this->promotion_project->findByPromotion($belong = 5, $version = 2, $limit = 4)->result(), //超值购
                'threeSales'    => $this->promotion_project->findByPromotion($belong = 7, $version = 2, $limit = 3)->result(), //促销
                'outingSales'   => $this->promotion_project->findByPromotion($belong = 8, $version = 2, $limit = 1)->result(), //出游季促销
                'news_content'  => $this->news_content->page_list($page_num = 10, $num = 0, $param = array())->result(),
            );
            $this->cache->memcached->save('wapindexHomePageCache', $data, 3 * 24 * 3600);
        } else {
            $data = $this->cache->memcached->get('wapindexHomePageCache');
        }
        $data['outingScenic'] = $this->promotion_project->findByPromotion($belong = 9, $version = 2, $limit = 1);//出游季景区产品
        if ($data['outingScenic']->num_rows() > 0) {
            $Scenic_id = $data['outingScenic']->row(0)->item_id;
            $data['outingScenic'] = $this->scenery_base->sceneryFindOne($sid = $Scenic_id);
        }
        //$indexScenic = $this->scenery_base->sceneryOnSal($getData = array(), $limit = 9);//为你精选 景区推荐
        $this->load->view('home/home/newindex', $data);
    }

    /**
     * 自在游
     * ajaxHomeScenery ajaxHomeHotel ajaxHomeLine ajaxHomeTourism
     */
    public function freeTravel()
    {
        $regionId = 383; //城市编号
        $regionName = get_cookie('city') ? get_cookie('city') : '杭州';
        $result = $this->region->getChinaRegion(array('region_type' => 2));
        foreach ($result->result() as $item) {
            if ($regionName == $item->region_name) {
                $regionId = $item->region_id;
                $regionName = $item->region_name;
                break;
            }
        }
        $data['allCities'] = $result->result();
        $data['cityId'] = $regionId; //城市ID
        $data['cityName'] = $regionName; //城市名称
        $data['currentUrl'] = current_url();

        $this->load->view('home/home/freeTravel', $data);
    }

    /**
     * 自在游 景区
     */
    public function ajaxHomeScenery()
    {
        $pg = $this->input->post('pg') ? $this->input->post('pg') : 1;
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $params['region_name'] = $this->input->post('city_name');
        $params['region_type'] = 2;
        $params['limit'] = 1;
        if (!empty($params['region_name'])) {
            $result = $this->region->findParams($params);
            if ($result->num_rows() > 0) {
                $params2['cityid'] = $result->row()->region_id;
            }
        }
        if (empty($params2['cityid'])) {
            $params2['cityid'] = "383";
        }
        $result = $this->scenery_base->search($params2, $page_num, $num);
        $data['scenery'] = $result;
        echo ($result->num_rows() > 0) ? json_encode(array('status' => true, 'count' => $result->num_rows(), 'content' => $this->load->view('home/home/ajaxHomeScenery', $data, true))) : json_encode(array('status' => false));
        exit;
    }

    /**
     * 自在游 酒店
     */
    public function ajaxHomeHotel()
    {
        $pg = $this->input->post('pg') ? $this->input->post('pg') : 1;
        $params2['size'] = 10;
        $params2['p'] = $pg;
        $params['region_name'] = $this->input->post('city_name');
        $params['region_type'] = 2;
        $params['limit'] = 1;
        if (!empty($params['region_name'])) {
            $result = $this->region->findParams($params);
            if ($result->num_rows() > 0) {
                $params2['city_id'] = $result->row()->region_id;
            }
        }
        if (empty($params2['city_id'])) {
            $params2['city_id'] = "383";
        }
        $result = json_decode(fn_get_contents($this->config->hotel_api_url . 'm/hotel/HotelList', $params2, 'post'));
        if (empty($result)) {
            $this->jsonMessage('数据异常');
        }
        if ($result->status === 0) {
            $data['hotel'] = $result->data->list;
            echo json_encode(array('status' => true, 'count' => count($result->data->list), 'content' => $this->load->view('home/home/ajaxHomeHotel', $data, true)));
            exit;
        } else {
            $this->jsonMessage($result->msg);
        }
    }

    /**
     * 自在游 线路
     */
    public function ajaxHomeLine()
    {

        $pg = $this->input->post('pg') ? $this->input->post('pg') : 1;
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $params['region_name'] = $this->input->post('city_name');
        $params['region_type'] = 2;
        $params['limit'] = 1;
        if (!empty($params['region_name'])) {
            $result = $this->region->findParams($params);
            if ($result->num_rows() > 0) {
                $params2['city_id'] = $result->row()->region_id;
            }
        }
        if (empty($params2['city_id'])) {
            $params2['city_id'] = "383";
        }
        $result = $this->line_base->findLineList($params2['city_id'], $page_num, $num);
        $data['line'] = $result;
        $data['line_travel_type'] = lineTravelType($type = true);
        echo ($result->num_rows() > 0) ? json_encode(array('status' => true, 'count' => $result->num_rows(), 'content' => $this->load->view('home/home/ajaxHomeLine', $data, true))) : json_encode(array('status' => false));
        exit;
    }

    /**
     * 自在游 特产
     */
    public function ajaxHomeTourism()
    {
        $pg = $this->input->post('pg') ? $this->input->post('pg') : 1;
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $params['region_name'] = $this->input->post('city_name');
        $params['region_type'] = 2;
        $params['limit'] = 1;
        if (!empty($params['region_name'])) {
            $result = $this->region->findParams($params);
            if ($result->num_rows() > 0) {
                $params2['city_id'] = $result->row()->region_id;
            }
        }
        if (empty($params2['city_id'])) {
            $params2['city_id'] = "383";
        }
        $result = $this->tourism_goods_base->findListByRegionId($page_num, $num, $params2['city_id']);
        $data['tourism'] = $result;
        echo ($result->num_rows() > 0) ? json_encode(array('status' => true, 'count' => $result->num_rows(), 'content' => $this->load->view('home/home/ajaxHomeTourism', $data, true))) : json_encode(array('status' => false));
        exit;
    }

    /**
     * 切换当前城市
     */
    public function freeTravelSetCity()
    {
        $params['region_name'] = $this->input->post('cityName') ? $this->input->post('cityName') : '杭州';
        $params['region_type'] = 2;
        $result = $this->region->findParams($params);
        if ($result->num_rows() > 0) {
            $region_id = $result->row()->region_id;
            $region_name = $result->row()->region_name;
        } else {
            $region_id = "383";
            $region_name = "杭州";
        }
        set_cookie('city', $region_name, 43250);
        set_cookie('cityid', $region_id, 43250);
        echo json_encode(array('status' => true));
        exit;
    }

    /**
     * @descripte   为你精选
     * @Author xiumao
     * @date 2016/7/27 上午 10:18
     */
    public function ajaxIndexTourism()
    {
        $pg = $this->input->get('pg');
        if ($pg == 1) {
            $data['indexTourism'] = $this->tourism_goods_base->findBySalesRank(12); //为你精选
            echo ($data['indexTourism']->num_rows() > 0) ? json_encode(array('status' => true, 'content' => $this->load->view('home/home/ajaxIndexTourism', $data, true))) : json_encode(array('status' => false));
            exit;
        } else {
            echo json_encode(array('status' => false));
            exit;
        }
    }

    /**
     * @descripte   周边游
     * @Author xiumao
     * @date 2016/7/13 下午 1:41
     */
    public function travelAround()
    {
        $data['headerBanner'] = $this->advert->findByParams(['status'=> 1, 'opentype' => 'link', 'position' => 16], '*', null, 'sort ASC'); //头部广告位
        $data['hotDestination'] = $this->promotion_project->findByPromotion($belong = 6, $version = 2, $limit = 6); //热门目的地
        $category = $this->scenic_category->find(true);
        foreach ($category->result() as $key => $val) {
            if (in_array($val->category_id, array(1, 2, 3, 4, 5, 7, 8, 11))) {
                $result[$key]['category_id'] = $val->category_id;
                $result[$key]['category_name'] = $val->category_name;
            }
        }
        $data['category'] = array_merge($result);//精选主题
        $regionName = get_cookie('city') ? get_cookie('city') : '杭州';
        $data['regionName'] = $regionName;//城市
        $region = $this->region->findByName($regionName);
        if ($region->region_type == 1) {
            $cities_id = $this->region->findByType($parent_id = $region->region_id, $region_type = 2);//取出省的所有城市
            if ($cities_id->num_rows() > 0) {
                foreach ($cities_id->result() as $key => $val) {
                    $param['cityId'][] = $val->region_id;
                }
            }
            $data['province_id'] = $region->region_id;
            $data['moreHotScenic'] = $this->scenery_base->sceneryRecommend($getData = array('provinceid' => $data['province_id']), $page_num = 20, $num = 0);//更多热门景点
        } else {
            $data['city_id'] = !empty($region) ? $region->region_id : 383;
            $data['moreHotScenic'] = $this->scenery_base->sceneryRecommend($getData = array('cityId' => $data['city_id']), $page_num = 20, $num = 0);//更多热门景点
        }
        $data['more'] = ($data['moreHotScenic']->num_rows() > 0) ? (($data['moreHotScenic']->num_rows() >= 20) ? true : false) : false;
        $this->load->view('home/home/travelAround', $data);
    }

    /**
     * @descripte   周边游 分页
     * @Author xiumao
     * @date 2016/7/13 下午 1:41
     */
    public function ajaxTravelAround()
    {
        $pg = trim($this->input->get('pg', true));
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $regionName = get_cookie('city') ? get_cookie('city') : '杭州';
        $data['regionName'] = $regionName;//城市
        $region = $this->region->findByName($regionName);
        if ($region->region_type == 1) {
            $cities_id = $this->region->findByType($parent_id = $region->region_id, $region_type = 2);//取出省的所有城市
            if ($cities_id->num_rows() > 0) {
                foreach ($cities_id->result() as $key => $val) {
                    $param['cityId'][] = $val->region_id;
                }
            }
            $param['province_id'] = $region->region_id;
            $data['moreHotScenic'] = $this->scenery_base->sceneryRecommend($getData = array('provinceid' => $param['province_id']), $page_num, $num);//更多热门景点
        } else {
            $param['city_id'] = !empty($region) ? $region->region_id : 383;
            $data['moreHotScenic'] = $this->scenery_base->sceneryRecommend($getData = array('cityId' => $param['city_id']), $page_num, $num);//更多热门景点
        }
        echo ($data['moreHotScenic']->num_rows() > 0) ? json_encode(array('status' => true, 'content' => $this->load->view('home/home/ajaxTravelAround', $data, true))) : json_encode(array('status' => false));
        exit;
    }

    /**
     * @descripte   城市
     * @Author xiumao
     * @date 2016/7/15 上午 10:37
     */
    public function setCity()
    {
        $result = $this->region->getChinaRegion(array('region_type' => 2));
        $data['allCities'] = $result->result();
        $data['currentUrl'] = current_url();
        $this->load->view('home/home/setCity', $data);
    }

    /**
     * 切换当前城市
     */
    public function changeCity()
    {
        $cityName = $this->input->get('cityName');
        if ($cityName) {
            set_cookie('city', $cityName, 43250);
        }
        redirect('home/home/travelAround');
    }

    /**
     * @descripte   着想公告
     * @Author xiumao
     * @date 2016/7/15 下午 4:26
     */
    public function notice()
    {
        $param['class_id'] = $this->input->get('class_id') ? trim($this->input->get('class_id')) : false;
        $data['news_class'] = $this->news_class->page_list($page_num = 20, $num = 0);
        $data['news_content'] = $this->news_content->page_list($page_num = 20, $num = 0, $param);
        $data['more'] = ($data['news_content']->num_rows() > 0) ? (($data['news_content']->num_rows() >= 20) ? true : false) : false;
        $this->load->view('home/home/notice', $data);
    }

    /**
     * @descripte   着想公告ajax
     * @Author xiumao
     * @date 2016/7/15 下午 4:26
     */
    public function ajaxNotice()
    {
        $param['class_id'] = $this->input->get('class_id') ? trim($this->input->get('class_id')) : false;
        $data['news_content'] = $this->news_content->page_list($page_num = 20, $num = 0, $param);
        $data['more'] = ($data['news_content']->num_rows() > 0) ? (($data['news_content']->num_rows() >= 20) ? true : false) : false;
        echo ($data['news_content']->num_rows() > 0) ? json_encode(array('status' => true, 'content' => $this->load->view('home/home/ajaxNotice', $data, true))) : json_encode(array('status' => false));
        exit;
    }

    /**
     * @descripte   着想公告ajax 分页
     * @Author xiumao
     * @date 2016/7/15 下午 4:26
     */
    public function ajaxNoticePage()
    {
        $pg = trim($this->input->get('pg', true));
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $param['class_id'] = $this->input->get('class_id') ? trim($this->input->get('class_id')) : false;
        $data['news_content'] = $this->news_content->page_list($page_num, $num, $param);
        echo ($data['news_content']->num_rows() > 0) ? json_encode(array('status' => true, 'content' => $this->load->view('home/home/ajaxNoticePage', $data, true))) : json_encode(array('status' => false));
        exit;
    }

    /**
     * @descripte   着想公告详情
     * @Author xiumao
     * @date 2016/7/15 0015 4:26
     */
    public function noticeDetail($id)
    {
        $result = $this->news_content->findById($id);
        if ($result->num_rows() > 0) {
            $data['noticeDetail'] = $result->row();
        }
        $param['id'] = $id;
        $param['class_id'] = $data['noticeDetail']->class_id;
        $data['tutor'] = $this->news_content->informationTutor($page_num = 4, $num = 0, $param);
        $this->load->view('home/home/noticeDetail', $data);
    }

    /**
     * @descripte    搜索
     * @Author xiumao
     * @date 2016/7/15 下午 4:28
     */
    public function search()
    {
        $data['historySearch'] = array();
        $hisCookie = get_cookie('historySearch');
        if (!empty($hisCookie)) {
            $data['historySearch'] = unserialize(get_cookie('historySearch'));
        }
        $data['hotSearch'] = $this->tourism_key_word->findByParams(['provider_id' => $this->providerUid, 'status' => 1]);
        $this->load->view('home/home/search', $data);
    }

    /**
     * @descripte   清除历史搜索
     * @Author xiumao
     * @date 2016/7/19 下午 6:35
     */
    public function delete_cookie()
    {
        delete_cookie('historySearch');
        echo json_encode(array(
            'status'   => true,
            'messages' => '清除成功',
        ));
    }

    /**
     * @descripte   抽奖
     * @Author xiumao
     * @date 2016/7/15 下午 4:28
     */
    public function lottery()
    {
        $data['activity_lottery'] = $this->activity_lottery->selectActivityList();
        $this->load->view('home/home/lottery', $data);
    }

    /**
     * @descripte   抽奖搜索
     * @Author xiumao
     * @date 2016/7/20 下午 3:56
     */
    public function ajaxLottery()
    {
        $keyword = (string)trim($this->input->get('keyword', true));
        if (isset($keyword) && $keyword != '') {
            $data['activity_lottery'] = $this->activity_lottery->activitySearch($keyword);
            echo ($data['activity_lottery']->num_rows() > 0) ? json_encode(array('status' => true, 'content' => $this->load->view('home/home/ajaxLottery', $data, true))) : json_encode(array('status' => false));
            exit;
        } else {
            echo json_encode(array('status' => false));
            exit;
        }
    }

    /**
     * ajax获取当前地址，并存入cookie
     */
    public function ajaxCurrentCity()
    {
        $location = $this->input->get('location');
        $coordtype = $this->input->get('coordtype');
        $output = $this->input->get('output');
        $pois = $this->input->get('pois');
        $cityName = locationCurrentCity($location, $coordtype, $output, $pois);
        if ($cityName) {
            set_cookie('city', $cityName['city'], 43200);
            set_cookie('province', $cityName['province'], 43200);
        }
        $this->ajaxJson(['status' => true, 'msg' => $cityName['city']]);
    }
}
